﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

using LowLevelGraphics.Histograms;

//TODO: Energy Map: implement and Test

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// 
    /// </summary>
    public class EnergyMap : BaseImageFilter
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="EnergyMap"/> class.
        /// </summary>
        public EnergyMap()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="EnergyMap"/> class.
        /// </summary>
        /// <param name="_energyMap">The _energy map.</param>
        internal EnergyMap(EnergyMap _energyMap)
        {
        }

        /// <summary>
        /// Execute this Filter
        /// A Debug Statement is written if a concrete Filter is called
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            //            histogram.SortBy(PROCESSING_MODE.GrayColor);

            GrayScale gray = new GrayScale();
            gray.Execute(_bitmap);
            int nWidth = _bitmap.Width;
            int nHeight = _bitmap.Height;

            int y = 0;
            int x = 0;

            Color color = Color.Transparent;
            int nGray = 0;
            int nGraySum = 0;

            int[] aValue = new int[256];

            int nGrayTemp = 0;
            Color[] aColor = new Color[8];
            for (y = 1; y < nHeight - 1; y++)
            {
                for (x = 1; x < nWidth - 1; x++)
                {
                    color = _bitmap.GetPixel(x, y);
                    nGray = ColorFunctions.Gray(color);
                    aColor = Segmentation.GetEnvironmentPixelColorArray(_bitmap, x, y, Definitions.EnvironmentPointListClockwise, aColor);

                    foreach (Color colorTemp in aColor)
                    {
                        nGrayTemp = ColorFunctions.Gray(colorTemp);
                        nGrayTemp = Math.Abs(nGray - nGrayTemp);
                        aValue[nGrayTemp] = aValue[nGrayTemp] + 1;
                    }
                }
            }

            for (int i = 0; i < aValue.Length; i++)
            {
                nGraySum += aValue[i] * aValue[i];
            }

            int nValue = 0;
            int nCount = 0;
            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    color = _bitmap.GetPixel(x, y);
                    nCount = aValue[ColorFunctions.Gray(color)];                    
                    _bitmap.SetGrayPixel(x, y, ImageMath.Clamp(nCount, 0, 255));
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// This is used to clone a filter, each filter must implement it's own clone functionality
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new EnergyMap(this);
        }
    }
}
